package org.example.rabbitmq.server.publish;

import org.example.rabbitmq.config.MultiQueueProperties;
import org.example.rabbitmq.server.route.strategy.RouteStrategy;
import org.example.rabbitmq.vo.RoutingKey;
import org.example.rabbitmq.vo.TenantId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EventPublisher {
    Logger logger = LoggerFactory.getLogger(EventPublisher.class);
    @Autowired
    RouteStrategy routeStrategy;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    MultiQueueProperties multiQueueProperties;

    public void publish(TenantId tenantId, String message) {
        RoutingKey routingKey = routeStrategy.route(tenantId);
        rabbitTemplate.convertAndSend(multiQueueProperties.getExchangeName(), routingKey.getRoutingKey(), message);
        logger.info("[P] message published with routingKey " + routingKey);
    }
}